<template>
    <div>
        <div>
            <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
                <el-breadcrumb-item :to="{ path: '/sys-admin/index' }">
                    <i class="el-icon-s-promotion"></i> 首页
                </el-breadcrumb-item>
                <el-breadcrumb-item :to="{ path: '/sys-admin/temp/admin/add-new' }">
                    添加用户
                </el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
        </div>
        <div style="margin:3% 15% 0 0 ; width: 70%">

            <el-form :model="AdminAddNew" :rules="rules" ref="AdminAddNew" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="AdminAddNew.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="AdminAddNew.password"></el-input>
                </el-form-item>
                <el-form-item label="昵称" prop="nickname">
                    <el-input v-model="AdminAddNew.nickname"></el-input>
                </el-form-item>
                <el-form-item label="头像URL" prop="avatar">
                    <el-input v-model="AdminAddNew.avatar"></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="AdminAddNew.phone"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="AdminAddNew.email"></el-input>
                </el-form-item>
                <el-form-item label="用户简介" prop="description">
                    <el-input v-model="AdminAddNew.description"></el-input>
                </el-form-item>
                <el-form-item label="是否启用" prop="delivery">
                    <el-switch
                            v-model="AdminAddNew.enable"
                            :active-value="1"
                            :inactive-value="0"
                            active-color="#13ce66"
                            inactive-color="#999"
                    ></el-switch>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('AdminAddNew')">立即创建</el-button>
                    <el-button @click="resetForm('AdminAddNew')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                AdminAddNew: {
                    username: '',
                    password:'',
                    nickname:'',
                    avatar:'',
                    phone:'',
                    email:'',
                    description:'',
                    enable: 1,
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 4, max: 12, message: '长度在 4 到 12 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 8, max: 15, message: '长度在 8 到 15 个字符', trigger: 'blur'}
                    ],
                    nickname:[
                        {required: true, message: '请输入昵称', trigger: 'blur'},
                        {min: 8, max: 15, message: '长度在 8 到 15 个字符', trigger: 'blur'}
                    ],
                    phone:[
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                        {min: 11, max: 11, message: '请正确输入手机号', trigger: 'blur'}
                    ],
                    email:[
                        {required: true, message: '请输入邮箱', trigger: 'blur'},
                        {min: 8, max: 18, message: '长度在 8 到 18 个字符', trigger: 'blur'}
                    ],
                    description: [
                        {required: true, message: '请输入简介', trigger: 'blur'},
                        {min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur'}
                    ],
                    sort: [
                        {pattern:/^(\d{1}|[1-9]{1}[0-9]?)$/,message:"排序号必须是0-99之间的数字",trigger:'blur'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        console.log(this.AdminAddNew)
                        this.axios.post("http://localhost:9081/admins/add-new",this.qs.stringify(this.AdminAddNew))
                            .then((response) =>{
                                console.log(response)
                                if (response.data.state==20000){
                                    this.$message.success("添加成功")
                                }else{
                                    this.$message.error(response.data.message)
                                }
                            })
                    } else {
                        console.log('请检查信息是否有误');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>